package com.zkyc.framework.user.service.impl;

import com.sun.corba.se.spi.oa.OADefault;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.user.Util.VerifyCodeUtils;
import com.zkyc.framework.user.config.JedisPoolFactory;
import com.zkyc.framework.user.service.ImageValidateCodeService;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Service
public class ImageValidateCodeServiceImpl implements ImageValidateCodeService {

    @Override
    public ResultVO getCode(String uuid) {

        //随机生成6位验证码
        String code = VerifyCodeUtils.generateVerifyCode
                (4).toLowerCase();

        //写入redis
        String  redisKey="verifyCode_"+uuid;
       JedisPoolFactory.setex(redisKey,120, code);


        //输出到图片
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            VerifyCodeUtils.outputImage(120, 40, data, code);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //把图片加密返回
        BASE64Encoder encoder = new BASE64Encoder();

        String encode = encoder.encode(data.toByteArray());
    return     ResultVO.data(encode);
    }
}